<?php

namespace app\admin\controller\basic;

use app\common\controller\Backend;
use think\Db;

class Xzsm extends Backend
{
    public function index()
    {
        //  $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();

        $db2 = Db::connect(config('database.xzsm'));
        $list = $db2->name('wanlshop_goods_sku')->alias('s')
            ->join('fa_wanlshop_goods g', 'g.id = s.goods_id')
            ->where($where)->whereNull('g.deletetime')->where('g.shop_id', 8)
            ->where('s.state',"0")
            ->order('s.id desc')->field('s.id,s.price,s.difference,g.title,g.category_id,
            s.stock,s.createtime,s.cost_price,s.state')
            ->paginate($limit)->each(function ($item) use ($db2) {
                $item['category_id'] = $db2->name('wanlshop_category')
                    ->where('id', $item['category_id'])->value('name');
                return $item;
            });

        $result = ['total' => $list->total(), 'rows' => $list->items()];

        return json($result);
    }


    public function edit($ids = null)
    {
        $db2 = Db::connect(config('database.xzsm'));
        $row = $db2->name('wanlshop_goods_sku')->alias('s')
            ->join('fa_wanlshop_goods g', 'g.id = s.goods_id')
            ->where('s.id', $ids)
            ->field('s.id,s.price,s.difference,g.title,g.category_id,s.stock,s.createtime,s.cost_price')->find();
        $row['category_id'] = $db2->name('wanlshop_category')
            ->where('id', $row['category_id'])->value('name');
        if (false === $this->request->isAjax()) {
            $this->assign('row', $row);
            return $this->view->fetch();
        }

        $params = $this->request->post('row/a');
        $db2->name('wanlshop_goods_sku')->where('id', $params['id'])->update(['price' => $params['price'],
            'cost_price' => $params['cost_price']]);

        $this->success();
    }
}